package main

import (
	"fmt"
)

/**
执行用时：12 ms, 在所有 Go 提交中击败了81.55%的用户
内存消耗：6.2 MB, 在所有 Go 提交中击败了37.50%的用户
 */
func setZeroes(matrix [][]int)  {

	n, m := len(matrix), len(matrix[0])

	r0, c0 := false, false
	for i := 0; i < m; i++ {
		if matrix[0][i] == 0 {
			r0 = true
			break
		}
	}
	for i := 0; i < n; i++ {
		if matrix[i][0] == 0 {
			c0 = true
			break
		}
	}

	for i := 1; i < n; i++ {
		for j := 0; j < m; j++ {
			if matrix[i][j] == 0 { matrix[i][0] = 0 }
		}
	}
	for i := 1; i < m; i++ {
		for j := 0; j < n; j++ {
			if matrix[j][i] == 0 { matrix[0][i] = 0 }
		}
	}

	for i := 1; i < n; i++ {
		if matrix[i][0] == 0 {
			for j := 0; j < m; j++ {
				matrix[i][j] = 0
			}
		}
	}
	for i := 1; i < m; i++ {
		if matrix[0][i] == 0 {
			for j := 0; j < n; j++ {
				matrix[j][i] = 0
			}
		}
	}

	if r0 {
		for i := 0; i < m; i++ {
			matrix[0][i] = 0
		}
	}
	if c0 {
		for i := 0; i < n; i++ {
			matrix[i][0] = 0
		}
	}
}

func main() {

	matrix := [][]int{
		{0, 1, 2, 0},
		{3, 4, 5, 2},
		{1, 3, 1, 5},
	}
	setZeroes(matrix)
	fmt.Println(matrix)
}
